package com.bage.finance.biz.service.impl;

import com.bage.common.exception.BizException;
import com.bage.common.service.TokenService;
import com.bage.finance.biz.config.ObjectConvertor;
import com.bage.finance.biz.domain.AssistCalculateDepartment;
import com.bage.finance.biz.dto.AdminDTO;
import com.bage.finance.biz.dto.form.CreateAssistCalculateDepartmentForm;
import com.bage.finance.biz.dto.form.UpdateAssistCalculateDepartmentForm;
import com.bage.finance.biz.dto.vo.BaseAssistCalculateVo;
import com.bage.finance.biz.dto.vo.GetAssistCalculateDepartmentVo;
import com.bage.finance.biz.dto.vo.ListAssistCalculateDepartmentVo;
import com.bage.finance.biz.enums.AssistCalculateCateCodeEnum;
import com.bage.finance.biz.mapper.AssistCalculateDepartmentMapper;
import com.bage.finance.biz.service.AssistCalculateHandleService;
import com.bage.mybatis.help.MyBatisWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.bage.finance.biz.domain.AssistCalculateDepartmentField.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class AssistCalculateDepartmentServiceImpl implements AssistCalculateHandleService {

    final ObjectConvertor objectConvertor;

    final TokenService<AdminDTO> tokenService;

    final AssistCalculateDepartmentMapper assistCalculateDepartmentMapper;

    @Override
    public AssistCalculateCateCodeEnum getAssistCalculateCateCodeEnum() {
        return AssistCalculateCateCodeEnum.DEPARTMENT;
    }

    @Override
    public boolean create(Object object) {
        CreateAssistCalculateDepartmentForm form = (CreateAssistCalculateDepartmentForm) object;
        AssistCalculateDepartment assistCalculateDepartment = objectConvertor.toAssistCalculateDepartment(form);
        assistCalculateDepartment.initDefault();
        assistCalculateDepartment.setMemberId(tokenService.getThreadLocalUserId());
        assistCalculateDepartment.setUpdateMemberId(tokenService.getThreadLocalUserId());
        assistCalculateDepartment.setTenantId(tokenService.getThreadLocalTenantId());

        return assistCalculateDepartmentMapper.insert(assistCalculateDepartment) > 0;
    }

    @Override
    public List<ListAssistCalculateDepartmentVo> listByAssistCalculateSummaryIds(List<Long> assistCalculateSummaryIds) {
        MyBatisWrapper<AssistCalculateDepartment> wrapper = new MyBatisWrapper<>();
        wrapper.select(Id, Manager, Phone, StartDate, RevokeDate, Disable, AssistCalculateSummaryId)
                .whereBuilder()
                .andEq(TenantId, tokenService.getThreadLocalTenantId())
                .andEq(DelFlag, false)
                .andIn(AssistCalculateSummaryId, assistCalculateSummaryIds);
        List<AssistCalculateDepartment> assistCalculateDepartments = assistCalculateDepartmentMapper.list(wrapper);
        return objectConvertor.toAssistCalculateDepartmentVo(assistCalculateDepartments);
    }

    @Override
    public GetAssistCalculateDepartmentVo get(Long assistCalculateSummaryId) {
        AssistCalculateDepartment assistCalculateDepartment = getById(assistCalculateSummaryId);
        GetAssistCalculateDepartmentVo result = objectConvertor.toGetAssistCalculateDepartmentVo(assistCalculateDepartment);
        return result;
    }

    /**
     * 查询部门辅助核算核算明细
     * @param id
     * @return
     */
    private AssistCalculateDepartment getById(long id) {
        MyBatisWrapper<AssistCalculateDepartment> wrapper = new MyBatisWrapper<>();
        wrapper.select(Id, Manager, StartDate, RevokeDate, Phone)
                .whereBuilder()
                .andEq(AssistCalculateSummaryId, id)
                .andEq(DelFlag, false)
                .andEq(TenantId, tokenService.getThreadLocalTenantId());
        return assistCalculateDepartmentMapper.get(wrapper);
    }

    @Override
    public boolean update(Object object) {
        UpdateAssistCalculateDepartmentForm form = (UpdateAssistCalculateDepartmentForm) object;
        MyBatisWrapper<AssistCalculateDepartment> wrapper = new MyBatisWrapper<>();
        wrapper.update(UpdateTime, new Date())
                .update(UpdateMemberId, tokenService.getThreadLocalUserId())
                .update(Manager, form.getManager())
                .update(Phone, form.getPhone())
                .update(StartDate, form.getStartDate())
                .update(RevokeDate, form.getRevokeDate())
                .whereBuilder()
                .andEq(AssistCalculateSummaryId, form.getId())
                .andEq(TenantId, tokenService.getThreadLocalTenantId())
                .andEq(DelFlag, false);
        if (assistCalculateDepartmentMapper.updateField(wrapper) == 0) {
            throw new BizException("修改失败");
        }
        return true;
    }
}
